/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.DisciplinaDao;
import Dao.DisciplinaDaoImplJPA;
import Dao.ProfessorDao;
import Dao.ProfessorDaoImplJPA;
import Dao.TurmaDao;
import Dao.TurmaDaoImplJPA;
import Pojo.Disciplina;
import Pojo.Professor;
import Pojo.Turma;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author thiago
 */
public class FormConsultaTurmas extends javax.swing.JInternalFrame {
     /**
     * Acesso necessário para o caso de chamar o cadastro de professore
     */
    private int acesso;
    
    /**
     * Objeto professor para acesso.
     */
    private Professor professorAcesso;
    
    /**
     * Getter do acesso
     * @return 
     */
    public int getAcesso() {
        return acesso;
    }
    
    /**
     * Setter de acesso
     * @param acesso 
     */
    private void setAcesso(int acesso){
        this.acesso = acesso;
    }

    public Professor getProfessorAcesso() {
        return professorAcesso;
    }

    private void setProfessorAcesso(Professor professorAcesso) {
        this.professorAcesso = professorAcesso;
    }
    
    /**
     * Creates new form FormConsultaTurmas
     */
    public FormConsultaTurmas(int acesso, Professor professor) {
        initComponents();
        this.buscarDisciplinas();
        this.buscarProfessores();
        this.setAcesso(acesso);
        this.setProfessorAcesso(professor);
        rbtProfessor.setSelected(true);
        rbtProfessorActionPerformed(null);
        if (acesso!=1){
            painelAdministrador.setVisible(false);
        }else{
            rbtEditar.setSelected(true);
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        rbtProfessor = new javax.swing.JRadioButton();
        rbtDisciplina = new javax.swing.JRadioButton();
        cbbProfessor = new javax.swing.JComboBox();
        cbbDisciplina = new javax.swing.JComboBox();
        lblAno = new javax.swing.JLabel();
        txtAno = new javax.swing.JTextField();
        lblPeriodo = new javax.swing.JLabel();
        txtPeriodo = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        btnPesquisar = new javax.swing.JButton();
        painelAdministrador = new javax.swing.JPanel();
        rbtEditar = new javax.swing.JRadioButton();
        rbtVisualizar = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblTurmas = new javax.swing.JTable();

        setClosable(true);
        setTitle("Consulta de Turmas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisar"));

        buttonGroup1.add(rbtProfessor);
        rbtProfessor.setText("Professor");
        rbtProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtProfessorActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbtDisciplina);
        rbtDisciplina.setText("Disciplina");
        rbtDisciplina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtDisciplinaActionPerformed(evt);
            }
        });

        lblAno.setText("Ano");

        lblPeriodo.setText("Período");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtDisciplina)
                    .addComponent(rbtProfessor))
                .addGap(14, 14, 14)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbDisciplina, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPeriodo)
                            .addComponent(lblAno))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtAno, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                            .addComponent(txtPeriodo)))
                    .addComponent(cbbProfessor, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtProfessor)
                    .addComponent(cbbProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtDisciplina)
                    .addComponent(cbbDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAno)
                    .addComponent(txtAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPeriodo)
                    .addComponent(txtPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnPesquisar, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnPesquisar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonGroup2.add(rbtEditar);
        rbtEditar.setText("Editar");

        buttonGroup2.add(rbtVisualizar);
        rbtVisualizar.setText("Visualizar");

        javax.swing.GroupLayout painelAdministradorLayout = new javax.swing.GroupLayout(painelAdministrador);
        painelAdministrador.setLayout(painelAdministradorLayout);
        painelAdministradorLayout.setHorizontalGroup(
            painelAdministradorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelAdministradorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelAdministradorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtEditar)
                    .addComponent(rbtVisualizar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        painelAdministradorLayout.setVerticalGroup(
            painelAdministradorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelAdministradorLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(rbtEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbtVisualizar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(painelAdministrador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(painelAdministrador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        tblTurmas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Ano", "Período", "Localização", "Professor", "Disciplina"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblTurmas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblTurmasMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblTurmas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        /**
     * Cria a lista de alunos no comboBox
     */
    private void buscarDisciplinas(){
        DisciplinaDao alunoDao = new DisciplinaDaoImplJPA();
        List<Disciplina> listaDisciplinas = alunoDao.pesquisarTodos();
        for(Disciplina disciplina : listaDisciplinas){
            cbbDisciplina.addItem(disciplina.getId()+" - "+disciplina.getNome());
        }
    }
    
    /**
     * Cria a lusta de professores no comboBox
     */
    private void buscarProfessores(){
        ProfessorDao professorDao = new ProfessorDaoImplJPA();
        List<Professor> listaProfessores = professorDao.pesquisarTodos();
        for(Professor professor : listaProfessores){
            cbbProfessor.addItem(professor.getId()+" - "+professor.getNome());
        }
    }
    
    private void rbtProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtProfessorActionPerformed
        cbbProfessor.setEnabled(true);
        cbbDisciplina.setEnabled(false);
        lblAno.setEnabled(false);
        lblPeriodo.setEnabled(false);
        txtAno.setEnabled(false);
        txtPeriodo.setEnabled(false);
    }//GEN-LAST:event_rbtProfessorActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        TurmaDao turmaDao = new TurmaDaoImplJPA();
        List<Turma> listaTurmas = null;
        DefaultTableModel model;
        ProfessorDao professorDao = new ProfessorDaoImplJPA();
        String professorString;
        String[] dados;
        DisciplinaDao disciplinaDao = new DisciplinaDaoImplJPA();
        String disciplinaString;
        
        if (rbtProfessor.isSelected()){
            professorString = (String) cbbProfessor.getSelectedItem();
            if (professorString.length()>0){
                dados = professorString.split(" - ");
                Professor professor = professorDao.pesquisarPorId(Integer.parseInt(dados[0]));
                listaTurmas = turmaDao.pesquisarPorProfessor(professor);
            }else{
                JOptionPane.showMessageDialog(null, "Informe um professor para consultar suas turmas.");
            }
        }else if (rbtDisciplina.isSelected()){
            disciplinaString = (String) cbbDisciplina.getSelectedItem();
            if ((disciplinaString.length()==0)||(txtAno.getText().length()==0)||(txtPeriodo.getText().length()==0)){
                JOptionPane.showMessageDialog(null, "Informe uma disciplina, um ano e um período.");
            }else{
                dados = disciplinaString.split(" - ");
                Disciplina disciplina = disciplinaDao.pesquisarPorId(Integer.parseInt(dados[0]));
                int ano;
                int periodo;
                try{
                    ano = Integer.parseInt(txtAno.getText());
                    periodo = Integer.parseInt(txtPeriodo.getText());
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, "Os campos ano e perído devem ser informados corretamente.");
                    return;
                }
                listaTurmas = turmaDao.pesquisarPorDisciplinaAnoPeriodo(disciplina, ano, periodo);
            }
        }else{
            JOptionPane.showConfirmDialog(null, "Informe o filtro da pesquisa.");
        }

        model = (DefaultTableModel) tblTurmas.getModel();
        Object[] linha = new Object[6];

        model.setRowCount(0);
        if (listaTurmas!= null){
            for (Object o: listaTurmas){
                Turma t = (Turma) o;
                linha[0] = String.valueOf(t.getId());
                linha[1] = t.getAno();
                linha[2] = t.getPeriodo();
                linha[3] = t.getLocalizacao();
                linha[4] = t.getProfessor().getId()+" - "+t.getProfessor().getNome();
                linha[5] = t.getDisciplina().getId()+" - "+t.getDisciplina().getNome();
                model.addRow(linha);
            }
        }
        tblTurmas.repaint();
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void tblTurmasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTurmasMouseClicked
        if (tblTurmas.getSelectedRow()>=0){
            int linha = tblTurmas.getSelectedRow();
            if (this.getAcesso()==1){
                if (rbtEditar.isSelected()){
                    FormCadastroTurmas formCadastroTurmas = new FormCadastroTurmas(Integer.parseInt(tblTurmas.getValueAt(linha,0).toString()));
                    this.getParent().add(formCadastroTurmas);
                    formCadastroTurmas.setVisible(true);
                }else if (rbtVisualizar.isSelected()){
                    VisualizarAlunosTurma visualizarAlunosTurma = new VisualizarAlunosTurma(Integer.parseInt(tblTurmas.getValueAt(linha,0).toString()));
                    this.getParent().add(visualizarAlunosTurma);
                    visualizarAlunosTurma.setVisible(true);
                }
            }else if (this.getAcesso()==2){
                String professorString = tblTurmas.getValueAt(linha, 4).toString();
                String[] dados = professorString.split(" - ");
                if (Integer.parseInt(dados[0]) == this.getProfessorAcesso().getId()){
                    VisualizarAlunosTurma visualizarAlunosTurma = new VisualizarAlunosTurma(Integer.parseInt(tblTurmas.getValueAt(linha,0).toString()));
                    this.getParent().add(visualizarAlunosTurma);
                    visualizarAlunosTurma.setVisible(true);
                }
            }
        }
    }//GEN-LAST:event_tblTurmasMouseClicked

    private void rbtDisciplinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtDisciplinaActionPerformed
        cbbProfessor.setEnabled(false);
        cbbDisciplina.setEnabled(true);
        lblAno.setEnabled(true);
        lblPeriodo.setEnabled(true);
        txtAno.setEnabled(true);
        txtPeriodo.setEnabled(true);
    }//GEN-LAST:event_rbtDisciplinaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPesquisar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cbbDisciplina;
    private javax.swing.JComboBox cbbProfessor;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAno;
    private javax.swing.JLabel lblPeriodo;
    private javax.swing.JPanel painelAdministrador;
    private javax.swing.JRadioButton rbtDisciplina;
    private javax.swing.JRadioButton rbtEditar;
    private javax.swing.JRadioButton rbtProfessor;
    private javax.swing.JRadioButton rbtVisualizar;
    private javax.swing.JTable tblTurmas;
    private javax.swing.JTextField txtAno;
    private javax.swing.JTextField txtPeriodo;
    // End of variables declaration//GEN-END:variables
}
